//
//  TouchGesture.m
//  PortmanTouch
//
//  Created by cclaan on 11/4/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TouchGesture.h"

#define MAX_POINTS 500

@implementation TouchGesture

@synthesize touch;

- (id) init
{
	self = [super init];
	if (self != nil) {
		
		pointIndex = 0;
		
		r = (arc4random() % 255) / 255.;
		g = (arc4random() % 255) / 255.;
		b = (arc4random() % 255) / 255.;
		
	}
	return self;
}


-(void) addPoint:(CGPoint)p {
	
	
	if ( !touchPoints ) {
		
		touchPoints = (float*)malloc(MAX_POINTS * sizeof(float));
		
	}
	
	
	if ( pointIndex >= MAX_POINTS - 2 ) {
		pointIndex = 0;
	}
	

	touchPoints[pointIndex++] = p.x;
	touchPoints[pointIndex++] = p.y;
	
	
	
}


-(void) render {
	
	if ( touchPoints ) {
		
		glVertexPointer(2, GL_FLOAT, 0, touchPoints);
		glEnableClientState(GL_VERTEX_ARRAY);
		
		glColor4f(r*0.4, g*0.4, b*0.4, 0.2);
		glDrawArrays(GL_LINE_STRIP, 0, (int)(pointIndex/2));
		
		
		glPointSize(4);
		glColor4f(r, g, b, 1.0);
		
		glDrawArrays(GL_POINTS, 0, (int)(pointIndex/2));
		
		//[self updatePoints];
		
	}
	
}

-(float*) getPoints {
	
	return touchPoints;
	
}

-(int) numPoints {
	return (int) (pointIndex/2);
}

- (void) dealloc
{
	free(touchPoints);
	[super dealloc];
}




@end
